using System.Collections;
using System.Collections.Generic;

namespace Generics
{
    public class MyGenericLinkedList<T> : IEnumerable<T>
    {
        public MyGenericLinkedListNode<T>? First { get; private set; }
        public MyGenericLinkedListNode<T>? Last { get; private set; }
        public MyGenericLinkedListNode<T> AddLast(T node)
        {
            var newNode = new MyGenericLinkedListNode<T>(node);
            if (First == null)
            {
                First = newNode;
                Last = First;
            }
            else
            {
                var previous = Last;
                Last!.Next = newNode;
                Last = newNode;
                Last.Prev = previous;
            }
            return newNode;
        }

        public IEnumerator<T> GetEnumerator()
        {
            MyGenericLinkedListNode<T>? current = First;
            while (current != null)
            {
                yield return current.Value;
                current = current.Next;
            }
        }

        IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();
    }
}